package com.tarena.security.admin.service.impl;


import com.tarena.security.admin.converter.AppConverter;
import com.tarena.security.admin.converter.DepartmentConverter;
import com.tarena.security.admin.entity.db.Department;
import com.tarena.security.admin.entity.param.DepartmentParam;
import com.tarena.security.admin.entity.query.DepartmentQuery;
import com.tarena.security.admin.errorenum.ResultEnum;
import com.tarena.security.admin.exception.SecurityAdminBusinessException;
import com.tarena.security.admin.persists.repository.DepartmentRepository;
import com.tarena.security.admin.service.IDepartmentService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ly
 * @since 2023-04-10
 */
@Service
public class DepartmentServiceImpl  implements IDepartmentService {

    @Resource
    private DepartmentRepository departmentRepository;

    @Resource
    private DepartmentConverter departmentConverter;

    private static final Logger logger= LoggerFactory.getLogger(DepartmentServiceImpl.class);

    @Override public void addNewDepartment(DepartmentParam param) {
        int count=departmentRepository.getDepartmentCountByName(param.getName());
        logger.info("查询结果为：{}",count);
        if (count>=1){
            throw new SecurityAdminBusinessException(ResultEnum.SYS_DEPARTMENT_ALREADY_EXISTS);
        }
        Department department= departmentConverter.convertParamToModel(param);
        count=departmentRepository.addNewDepartment(department);
        if (count!=1){
            throw new SecurityAdminBusinessException(ResultEnum.SYSTEM_ERROR);
        }
    }

    @Override public List<Department> getDepartmentList(DepartmentQuery query) {

        List<Department> list =departmentRepository.getDepartmentList(query);
        Map<Integer, List<Department>> map = list.stream().collect(Collectors.groupingBy(Department::getParentId));

        if (CollectionUtils.isEmpty(map)) {
            return list;
        }
        List<Department> firstDepartmentList = map.get(0);
        if (CollectionUtils.isEmpty(firstDepartmentList)) {
            return list;
        }
        for (Department department : firstDepartmentList) {
            list.add(department);
            List<Department> secondDepartmentList = map.get(department.getId());
            if (CollectionUtils.isEmpty(secondDepartmentList)) {
                continue;
            }
            department.setChild(secondDepartmentList);
            for (Department secondDepartment : department.getChild()) {
                List<Department> thirdAreaList = map.get(secondDepartment.getId());
                secondDepartment.setChild(thirdAreaList);
            }
        }
        return list;
    }

    @Override public void updateByID(DepartmentParam param) {
        logger.info("DepartmentParam:{}",param);

        Department department=departmentRepository.getDepartmentById(param.getId());

        if (department==null){
            throw new SecurityAdminBusinessException(ResultEnum.SYS_DEPARTMENT_NON_EXISTENT);
        }
        int count = departmentRepository.getDepartmentCountByName(param.getName());
        if (count!=0){
            throw new SecurityAdminBusinessException(ResultEnum.SYS_DEPARTMENT_ALREADY_EXISTS);
        }
        department= departmentConverter.convertParamToModel(param);
        logger.info("Department:{}",department);

        int row=departmentRepository.updateByID(department);
        if (row!=1){
            throw new SecurityAdminBusinessException(ResultEnum.SYSTEM_ERROR);
        }
    }

    @Override public Department getDepartmentById(Integer id) {
        return departmentRepository.getDepartmentById(id);
    }

    @Override public void deleteDepartmentById(Integer[] id) {

        int i=departmentRepository.deleteById(id);
    }
}
